var pnum = "";  
var initData = function(num){
	    var param = {
			page_num:num,
			page_size:"10"
	    }
		$.ajax({
	        url: base + "/dataCenter/businessdata/findRescueFacility",
	        data: param,
	        success: function (data, textStatus, jqXHR) {
	        	if(data != null){
		        	var pageCount = data.pager.pageCount;
		        	var dataObj ={
		        			"list":data.list,
		         			"dataTool":{
		         				"title":{
		          			      	"city": "城市"
		          			      	,"deposit_location": "存放位置"
		          			      	,"explains":"说明"
		          			      	,"operation":"操作"
		            			},
		            			"titleCheckbox":false,
		    					"btn":[{"event":"updateObj","name":"编辑"},
		    					       {"event":"delObj","name":"删除"}]
							}
			  		};
		        	showPageTable('view', dataObj,num,pageCount,"initData");
	        	}
	        },
	        error: function () {
	        }
	    });
}

var searchObj = function(num){
    var param = {
			page_num:num,
			page_size:"10",
			city:$.trim($("#searchName").val())
	    }
		$.ajax({
	        url: base + "/dataCenter/businessdata/findRescueFacility",
	        data: param,
	        success: function (data, textStatus, jqXHR) {
	        	if(data != null){
		        	var pageCount = data.pager.pageCount;
		        	var dataObj ={
		        			"list":data.list,
		         			"dataTool":{
		         				"title":{
		          			      	"city": "城市"
		          			      	,"deposit_location": "存放位置"
		          			      	,"explains":"说明"
		          			      	,"operation":"操作"
		            			},
		            			"titleCheckbox":false,
		    					"btn":[{"event":"updateObj","name":"编辑"},
		    					       {"event":"delObj","name":"删除"}]
							}
			  		};
		        	showPageTable('view', dataObj,num,pageCount,"searchObj");
	        	}
	        },
	        error: function () {
	        }
	    });
}

//添加合成模板资料
var addData = function(){
		var contentDiv = function(){
			var content;
			var data={
					"title":{
						    "city":"城市",
					      	"deposit_location": "存放位置",
					      	"explains":"说明"
					},"text":{
						    "city":""
						    ,"deposit_location":""
							,"explains":""
					},"input":{
						    "city":"text"
						    ,"deposit_location":"text"
							,"explains":"text"
					},"input_init":{
					}
			};
			var getDiv =  dc_update.innerHTML;
			laytpl(getDiv).render(data, function(html){
				content = html;
			}); 
			return content;
		}
		layopen(contentDiv());
}

//编辑提出资料
var updateObj = function(obj){
	   var contentDIV = function(){
		   var content;
		   var data = {
				   "title":{
					   "id":"主键",
					   "city":"城市",
					   "deposit_location":"存放位置",
					   "explains":"说明"
				   },"text":{
					   "id":obj.id,
					   "city":obj.city,
					   "deposit_location":obj.deposit_location,
					   "explains":obj.explains
				   },"input":{
					   "id":"hidden",
					   "city":"text",
					   "deposit_location":"text",
					   "explains":"text"
				   },"input_init":{
				   }
		   };
		   var getDiv = dc_update.innerHTML;
		   laytpl(getDiv).render(data,function(html){
			   content = html;
		   });
		   return content;
	   }
	   layopen(contentDIV());
}

//打开弹窗
var layopen = function(content){
	layer.open({
	  	title:'更新救援设备库'
		,content: content
  	    ,area: ['800px', '600px']
	    ,scrollbar: false
	  	,btn: ['确认', '取消']
		,yes: function(index,layero){ //确认
		  	var data = $(layero).find("form").serializeJSON();
		  	if(verification_data(data)){
				$.post(base+"/dataCenter/businessdata/updateRescueFacility",data,
				  		function (data,status){
							if(data!=""&&data!=null){
								if(data.statusCode == 0){
									layer.msg('更新成功', {icon: 1,time:2000});
								}else{
									layer.msg(data.message, {icon: 2,time:2000});
								}
							}else{
								layer.msg('更新失败!', {icon: 2,time:2000});
							}
							initData(pnum);
				  		 })
		  	}
		}
});
}

//验证数据
var verification_data = function(data){
	if(data.city == ""){
		var obj = $("input[name='city']");
		obj.focus();
		layer.tips('请填写城市', obj);
		return false;
	}
	if(data.deposit_location == ""){
		var obj = $("input[name='deposit_location']");
		obj.focus();
		layer.tips('请填写存放位置', obj);
		return false;
	}
	if(data.explains == ""){
		var obj = $("input[name='explains']");
		obj.focus();
		layer.tips('请填写说明', obj);
		return false;
	}
	return true;
}


var delObj = function(obj){
	   layer.confirm('确认删除?',{btn : [ '确定', '取消' ]},function(index){
		   layer.close(index);
		   var param = {id:obj.id}
		   $.ajax({
			   url:base+"/dataCenter/businessdata/delRescueFacilityById",
			   data:param,
			   success:function(data,status){
				    if(data.statusCode == 0){
						layer.msg(data.message, {
							icon : 1
						});
				    }else{
						layer.msg(data.message, {
							icon : 5
						});
				    }
				    initData(pnum);
			   }
		   });
	   });
}
$("#addData").click(addData);

$(document).ready(function(){
	 initData(1);
});